/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GUIDStorage;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.player.BattlePlayer;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.player.SimplePlayer;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world.Chessboard;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.EventManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.InvalidPointSettingEvent;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.StartingPositionEvent;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * @author Massimo Canonaco
 *
 */
public class RemoveStartingPointAction extends AbstractCharacterAction {

	/** */ 
	static public final float GUID = GUIDStorage.REMOVE_STARTING_POINT_ACTION;
	
	/** */
	private StartingPositionEvent event;
	
	public RemoveStartingPointAction(SimplePlayer performer, BattlePlayer battlePerformer, Chessboard arena) 
	{
		super(performer, arena);
		event = new StartingPositionEvent();
		event.setPerformer(performer);
		event.setAdd(false);
	}

	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.GenericLogicAction#getActionIdentifier()
	 */
	@Override
	public float getActionIdentifier() 
	{
		return GUID;
	}

	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.GenericLogicAction#execute(java.util.LinkedList)
	 */
	@Override
	public boolean execute(LinkedList<GenericActionInfo> infos) 
	{
		// Settings the informations.
		settings(infos);
		
		if(check())
		{
			end = false;
			// If the targetPoint is changed and the character was on another cell.
			if(park.isPawnOn(character.getRow(), character.getColumn(), character))
			{
				// Drop him from the old cell.
				park.dropPawn(character.getRow(), character.getColumn());
				event.setPoint(character.getRow(), character.getColumn());
				event.setSubject(character);
				character.setPosition(Character.NO_POSITION, Character.NO_POSITION);
				
				/*
				 * park.dropPawn(character.getRow(), character.getColumn());
				event.setAdd(false);
				StartingPositionEvent newEvent = new StartingPositionEvent();
				newEvent.setPoint(character.getRow(), character.getColumn());
				character.setPosition(Character.NO_POSITION, Character.NO_POSITION);
				newEvent.setAdd(false);
				newEvent.setSubject(character);
				 */
				EventManager.getInstance().triggerEvent(event);
				end = true;
				return true;
			}
			
			return false;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.GenericLogicAction#reset()
	 */
	@Override
	public void reset() 
	{
		character = null;
		targetPoint = null;
		end = true;
	}

	/** */
	private void settings(LinkedList<GenericActionInfo> informations)
	{
		GenericActionInfo tempAction = null;
		float tempGUID = -1;
		
		Iterator<GenericActionInfo>iterator = informations.iterator();
		
		while(iterator.hasNext())
		{
			tempAction = iterator.next();
			tempGUID = tempAction.getGUID();
			
			if(tempGUID == AddCharacterInfo.GUID)
			{
				iterator.remove();
				
				Character content = ((AddCharacterInfo)tempAction).getCharacter();
				
				if(content.getOwner() == performer)
				{
					character = content;
				}
				else
				{
					EventManager.getInstance().triggerEvent(new InvalidPointSettingEvent(true));
				}
			}
			else if(tempGUID == ActionCoordinateInfo.GUID)
			{
				if(character != null)
				{
					iterator.remove();
					
					targetPoint = ((ActionCoordinateInfo) tempAction).getPoint();
					// If the cell is not a spawn cell or it's not free.
					if(park.getPawn(targetPoint.row, targetPoint.column) != null && park.getPawn(targetPoint.row, targetPoint.column) != character)
					{
						EventManager.getInstance().triggerEvent(new InvalidPointSettingEvent(false, targetPoint.row, targetPoint.column));
//						boolean b = park.getPawn(targetPoint.row, targetPoint.column) != character;
//						System.out.println("ERRORE NEL SET DEL PUNTO INIZIALE : "+b+ "da park : "+park.getPawn(targetPoint.row, targetPoint.column)+" "+character);
						targetPoint = null; 
					}
				}
			}
		}
	}
	
	/**Checks if all the required values are set.
	 * @return True, it's all set; false otherwise. */
	private boolean check() 
	{
		return character != null && targetPoint != null ;//&& performer.getCharacters().size() <= MatchManager.MAX_CHARACTERS_PER_PLAYER;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "REMOVE";
	}
}
